package com.opengroupe.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.opengroupe.R;
import com.opengroupe.dto.QuestionDto;
import com.opengroupe.dto.SingleReponse;
import com.opengroupe.dto.SingleQuestionReponse;

public class ResultatDetailleActivity extends Activity {
	
	private TextView txtContenuQuestion;
	private ListView lvReponses;

	private  SingleQuestionReponse sqr;
	
	private String tag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resultat_detaille);
        tag = getString(R.string.log_prefix);
        sqr = (SingleQuestionReponse) getIntent().getExtras().get("sqrSelected");
        
        //txtContenuQuestion
        this.txtContenuQuestion = (TextView)findViewById(R.id.txtContenuQuestion);
        this.txtContenuQuestion.setText(sqr.getQuestion().getContenuQuestion());
        
        //lvReponses
        this.lvReponses = (ListView)findViewById(R.id.lvReponses);        
        //this.lvReponses.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sqr.getReponsesCorrectes()));
		
        this.lvReponses.setAdapter(new ArrayAdapter<SingleReponse>(this, android.R.layout.simple_list_item_1, sqr.getReponses()) {
           	public View getView(int position, View convertView, ViewGroup parent) {
           		View v = convertView;
           		
           		if (v == null)
           		{
           			LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater();
                       v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
           		}
           		
           		if (v.getTag() != null) return v;
           		
           		SingleReponse reponse = this.getItem(position);
           		
           		v.setTag(reponse);
           		
           		TextView tvItem = (TextView) v.findViewById(android.R.id.text1);
           		tvItem.setText(reponse.getReponse().getContenuReponse());
           		
           		
           		
           		if (reponse.getIsReponseCandidat())
           		{
           			v.setBackgroundResource(R.drawable.bords_orange);
           			
           			if (!reponse.getReponse().getCorrect())
               		{
           				tvItem.setBackgroundColor(Color.RED);
               		}
           		}
           		
       			if (reponse.getReponse().getCorrect())
           		{
       				tvItem.setBackgroundColor(Color.GREEN);
           		}
       		
           	
       			return v;
           		
           	}
        });
           		
        
        Log.d(tag+" : contenuQuestion = ",sqr.getQuestion().getContenuQuestion());
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_resultat_detaille, menu);
        return true;
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
	

	
	
	
}
